<?php

	// +----------------------------------------------------------------------
	// | 访问记录
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\event\ucenter;

	use app\common\event\Base;
	use app\model\ucenter\User as UserModel;
	use app\model\ucenter\Visit as VisitModel;
	use app\event\ucenter\User as UserEvent;
	use app\model\store\Good as GoodModel;

	class Visit extends Base
	{

		public function add()
		{
			$module =request()->auth_module;
			// 访客记录
			$url = $this->request->url(true);// addslashes($_SERVER['PATH_INFO']);
			$refer = (isset($_SERVER['HTTP_REFERER'])) ? addslashes($_SERVER['HTTP_REFERER']) : '--';
			$visit = VisitModel::where('module', $module)
				->where('controller', $this->request->controller())
				->where('action', $this->request->action())
				->where('method', $this->request->method())
				->find();
			if (!$visit) {
				$add['module'] = $module;
				$add['action'] = $this->request->action();
				$add['controller'] = $this->request->controller();
				$add['method'] = $this->request->method();
				$visit = VisitModel::create($add);
			} else {
				$visit['hit'] = $visit['hit'] + 1;
				$visit->save();
			}
			if (1 == $visit['need_record']) {
				// 按天统计
				$log = $visit->logs()->where('url', $url)
					->where('ip', $this->request->ip())
					->whereTime('create_time', 'today')
					->find();
				if ($log) {
					$log['hit'] = $log['hit'] + 1;
					$log->save();
				} else {
					$log['hit'] = 1;
					$log['refer'] = $refer;
					$log['agent'] = $this->getBrowser();
					$user = UserEvent::getLogin();
					if ($user) {
						$log['user_id'] = $user['id'];
					}
					$log['url'] = $url;
					// 附属信息
					if (in_array($visit['id'], [1, 10, 11])) {
						// 店铺首页记录
						$store = UserModel::find(code_decode($this->request->param('code')));
						if ($store) {
							$log['store_id'] = $store['id'];
						}
					} elseif (in_array($visit['id'], [16])) {
						// 商品
						$good = GoodModel::where('status', 1)
							->where('sn', $this->request->param('code'))
							->find();
						if ($good) {
							$log['good_id'] = $good['id'];
							$log['store_id'] = $good['user_id'];
						}
					}
					$visit->logs()->save($log);
				}
			}
		}

		//返回浏览器版本
		private function getBrowser()
		{
			if (strpos($_SERVER['HTTP_USER_AGENT'], 'Maxthon')) {
				$browser = 'Maxthon';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 12.0')) {
				$browser = 'IE12.0';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 11.0')) {
				$browser = 'IE11.0';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 10.0')) {
				$browser = 'IE10.0';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 9.0')) {
				$browser = 'IE9.0';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.0')) {
				$browser = 'IE8.0';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.0')) {
				$browser = 'IE7.0';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.0')) {
				$browser = 'IE6.0';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'NetCaptor')) {
				$browser = 'NetCaptor';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape')) {
				$browser = 'Netscape';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Lynx')) {
				$browser = 'Lynx';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera')) {
				$browser = 'Opera';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome')) {
				$browser = 'Google';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
				$browser = 'Firefox';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari')) {
				$browser = 'Safari';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'iphone') || strpos($_SERVER['HTTP_USER_AGENT'], 'ipod')) {
				$browser = 'iphone';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'ipad')) {
				$browser = 'iphone';
			} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'android')) {
				$browser = 'android';
			} else {
				$browser = 'other';
			}
			return addslashes($browser);
		}
	}
